package com.example.administrator.ceshi0225;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.Image;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.view.View.INVISIBLE;

//import static com.sleepyzzz.photo_selector.activity.PhotoPickerActivity.actionStart;

public class MainActivity extends AppCompatActivity {
    private RelativeLayout relativeLayout;
    private View mErrorView;
    private TextView t1;
    private Button btn;
    private WebView webView;
    private ImageView ivError;
    private byte[] test = new byte[]{};
    private List list8 = new ArrayList() {
    };
    private IntentFilter intentFilter;
    private NetworkChangeReceiver networkChangeReceiver;
    private static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 104;
    public static final String url = "http://192.168.31.123/thinkphp1/index.php/Image/upload";
    public  final  String Action="android.net.conn.CONNECTIVITY_CHANGE";
    //  test=new byte[]{(byte)0xA0,(byte)0x04,(byte)0x01,(byte)0x74,(byte)0x00,(byte)0xE6};
    private String[] list7 = new String[]{"(byte)0xA0,(byte)0x04,0x01,0x74,0x00,0xE7", "0xA0,0x04,0x01,0x74,0x00,0xE6", "0xA0,0x04,0x01,0x74,0x00,0xE5"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//
//        if (!NetworkUtil.isNetworkAvailable(this)) {
//            Toast.makeText(this, "网络bu可用...", Toast.LENGTH_SHORT).show();
//           showErrorPage();
//           webView.setVisibility(View.INVISIBLE);
//
//        } else {
//            Toast.makeText(this, "网络可用...", Toast.LENGTH_SHORT).show();
//        }





        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);
        webView = findViewById(R.id.id);
        relativeLayout = findViewById(R.id.online_error_btn_retry);
// ivError=findViewById(R.id.ivError);
        //      t1=findViewById(R.id.tet);
        webView.setWebViewClient(new WebViewClient() {

            //在开始加载网页时会回调
            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
//                ivError.setVisibility(View.INVISIBLE);
                //   t1.setVisibility(View.INVISIBLE);
//  `                   relativeLayout.setVisibility(View.INVISIBLE);
                webView.setVisibility(View.VISIBLE);
            }


//            public void onPageStarted(WebView webView,String s,Bitmap bitmap){
//                super.onPageStarted(webView,s,bitmap);
//            }


            //加载错误的时候会回调
    //        @Override
//            public void onReceivedError(final WebView webView, int i, String s, String s1) {
//                super.onReceivedError(webView, i, s, s1);
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                    return;
//                }
////                ivError.setVisibility(View.VISIBLE);
//                //   t1.setVisibility(View.INVISIBLE);
//                //    t1.setVisibility(View.VISIBLE);
//
//
//                //     relativeLayout.setVisibility(View.VISIBLE);
//                showErrorPage();
//                webView.setVisibility(View.INVISIBLE);
////                t1.setOnClickListener(new View.OnClickListener() {
////                    @Override
////                    public void onClick(View v) {
////                        webView.reload();
////                    }
////                });
//            }

            //加载错误的时候会回调
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
                super.onReceivedError(webView, webResourceRequest, webResourceError);
                if (webResourceRequest.isForMainFrame()) {
//                    ivError.setVisibility(View.VISIBLE);
                    //   t1.setVisibility(View.VISIBLE);

                    showErrorPage();


                    webView.setVisibility(INVISIBLE);

                }
            }

            //加载完成的时候会回调
            @Override
            public void onPageFinished(WebView webView, String s) {

            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null) return false;
                try {
                    if (!url.startsWith("http://") && !url.startsWith("https://")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        return true;
                    }

                } catch (Exception e) {
                    return true;
                }
                view.loadUrl(url);
                return true;
            }



        });
        webView.getSettings().setJavaScriptEnabled(true);


        if (!NetworkUtil.isNetworkAvailable(this)) {
            Toast.makeText(this, "网络bu可用...", Toast.LENGTH_SHORT).show();
           showErrorPage();
           webView.setVisibility(View.INVISIBLE);

        } else {
            Toast.makeText(this, "网络可用...", Toast.LENGTH_SHORT).show();
        }

    }

    protected void showErrorPage() {
        FrameLayout webParentView = (FrameLayout) webView.getParent();
        initErrorPage();//初始化自定义页面
        while (webParentView.getChildCount() > 1) {
            webParentView.removeViewAt(0);
        }
        @SuppressWarnings("deprecation")
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewPager.LayoutParams.FILL_PARENT, ViewPager.LayoutParams.FILL_PARENT);
        webParentView.addView(mErrorView, 0, lp);
        //    mIsErrorPage = true;
    }

    protected void initErrorPage() {

        mErrorView = View.inflate(this, R.layout.activity_error, null);
        RelativeLayout layout = (RelativeLayout) mErrorView.findViewById(R.id.online_error_btn_retry);
//            LinearLayout lay=mErrorView.findViewById(R.id.layout);
        //    LinearLayout lay1=mErrorView.findViewById(R.id.layout1);
        layout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                webView.reload();
            }
        });
//        lay.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                webView.reload();
//            }
//        });
//        lay1.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                webView.reload();
//            }
//        });
        //   mErrorView.setOnClickListener(null);
    }

   public class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager manager = (ConnectivityManager) context
                    .getApplicationContext().getSystemService(
                            Context.CONNECTIVITY_SERVICE);
           // ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            NetworkInfo networkinfo = manager.getActiveNetworkInfo();
//            if (manager != null) {
//                //  return false;
//
//
//                if (networkinfo == null || !networkinfo.isAvailable()) {
//                    showErrorPage();
//                    webView.setVisibility(View.INVISIBLE);
//                    Toast.makeText(MainActivity.this,"meiwang",Toast.LENGTH_SHORT).show();
//                } else {
//                   webView.loadUrl("http://www.baidu.com");
//                }
//            }



                //  Log.e("wllj", ""+networkInfo.isAvailable());
//         if (networkInfo!=null){
//             Toast.makeText(MainActivity.this,"有望啊",Toast.LENGTH_SHORT).show();
//         }else {
//             Toast.makeText(MainActivity.this,"wuwang",Toast.LENGTH_SHORT).show();
//
//         }


        /*    if (networkInfo != null && networkInfo.isAvailable()) {
                webView.loadUrl("http://www.baidu.com");


            }else {
                showErrorPage();
                webView.setVisibility(View.INVISIBLE);

            }*/

        }
    }
}